package com.code.google.p.jyve.berekeningen;

class DriewaardigZwakkeBerekening extends ZwakkeBerekening {

	@Override
	public double berekenVanuitTitrant(double titrant) {
		double pH = 7; 
		double volTot = (mVolumeOplossing + titrant) / 1e3;
		titrant /= 1e3;
		
		if (mTypeStof == ZUUR){
			//stap 1
			if (mMolariteitOplossing * mVolumeOplossing/1e3 > mMolariteitTitrant * titrant){
				double a = 1;
				double b = volTot * mKz[0] - mMolariteitTitrant * titrant + (1e-7) * mVolumeOplossing/1e3;
				double c = -mMolariteitOplossing * mVolumeOplossing/1e3 * volTot * mKz[0];

				double x = abcMax(a, b, c);

				pH = -Math.log10((x - mMolariteitTitrant * titrant + (1e-7) * mVolumeOplossing/1e3) / volTot);
			}else{
				//stap 2
				if (2 * mMolariteitOplossing * mVolumeOplossing/1e3 > mMolariteitTitrant * titrant){
					double a = 1; 
					double b = -mMolariteitTitrant * titrant + mMolariteitOplossing * mVolumeOplossing/1e3 + mKz[1]*volTot;
					double c = -mKz[1] * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

					double x = abcMax(a, b, c);
					pH = -Math.log10((x  - mMolariteitTitrant * titrant + mMolariteitOplossing * mVolumeOplossing/1e3)/(volTot));
				}else{
					//stap 3
					if (3 * mMolariteitOplossing * mVolumeOplossing/1e3 > mMolariteitTitrant * titrant){
						double a = 1; 
						double b = -mMolariteitTitrant * titrant + 2 * mMolariteitOplossing * mVolumeOplossing/1e3 + mKz[2]*volTot;
						double c = -mKz[2] * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

						double x = abcMax(a, b, c);
						pH = -Math.log10((x  - mMolariteitTitrant * titrant + 2 * mMolariteitOplossing * mVolumeOplossing/1e3)/(volTot));
					}
					//verder
					else{
						double kb = kzToKb(mKz[2]);

						double a = 1;
						double b = mMolariteitTitrant * titrant - 3 * mMolariteitOplossing * mVolumeOplossing/1e3 + volTot * kb;
						double c = -kb * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

						double x = abcMax(a, b, c);

						pH = -Math.log10(KW) + Math.log10((x + mMolariteitTitrant * titrant - 3 * mMolariteitOplossing * mVolumeOplossing/1e3)/volTot);
					}
				}
			}
		}else{//BASE dus
			//stap 1
			if (mMolariteitOplossing * mVolumeOplossing/1e3 > mMolariteitTitrant * titrant){
				double a = 1;
				double b = -mMolariteitTitrant * titrant + mVolumeOplossing/1e3 * 1e-7 + volTot * mKz[0];
				double c = -mKz[0] * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;
				
				double x = abcMax(a, b, c);
				
				pH = -Math.log10(KW) + Math.log10((x  - mMolariteitTitrant * titrant + mMolariteitOplossing * mVolumeOplossing/1e3)/(volTot));
			}else{
				//stap 2
				if (2 * mMolariteitOplossing * mVolumeOplossing/1e3 > mMolariteitTitrant * titrant){
					double a = 1; 
					double b = -mMolariteitTitrant * titrant + mMolariteitOplossing * mVolumeOplossing/1e3 + mKz[1]*volTot;
					double c = -mKz[1] * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

					double x = abcMax(a, b, c);
					pH = -Math.log10(KW) + Math.log10((x  - mMolariteitTitrant * titrant + mMolariteitOplossing * mVolumeOplossing/1e3)/(volTot));
				}else{
					//stap 3
					if (3 * mMolariteitOplossing * mVolumeOplossing/1e3 > mMolariteitTitrant * titrant){
						double a = 1; 
						double b = -mMolariteitTitrant * titrant + 2 * mMolariteitOplossing * mVolumeOplossing/1e3 + mKz[2]*volTot;
						double c = -mKz[2] * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

						double x = abcMax(a, b, c);
						pH = -Math.log10(KW) + Math.log10((x  - mMolariteitTitrant * titrant + 2 * mMolariteitOplossing * mVolumeOplossing/1e3)/(volTot));
					}
					//verder
					else{
						double kz = kzToKb(mKz[1]);

						double a = 1;
						double b = mMolariteitTitrant * titrant - 3 * mMolariteitOplossing * mVolumeOplossing/1e3 + volTot * kz;
						double c = -kz * volTot * mMolariteitOplossing * mVolumeOplossing/1e3;

						double x = abcMax(a, b, c);

						pH = -Math.log10((x + mMolariteitTitrant * titrant - 3 * mMolariteitOplossing * mVolumeOplossing/1e3)/volTot);
					}
				}
			}
		}
		return pH;
	}

}
